<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Contacts for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Contacts\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Contacts\Model\Person;          // <-- Add this import
use Contacts\Form\ContactForm;
use Contacts\Form\DetailsForm;
use Contacts\Model\ContactView;
use Contacts\Model\ContactViewTable; 
use Contacts\Model\Contacts;
use Contacts\Model\ContactsTable;

class ContactsController extends AbstractActionController
{
	protected $ContactViewTable;
	protected $personTable;
	protected $detailsTable;
	
	public function indexAction()
     {
     	return new ViewModel(array(
		//lead s ada 2
     			'contactview' => $this->getContactViewTable()->fetchAll(),
     	));
     }
      public function getContactViewTable()
    {
    	if (!$this->ContactViewTable) {
    		$sm = $this->getServiceLocator();
    		$this->ContactViewTable = $sm->get('Contacts\Model\ContactViewTable');
    	}
    	return $this->ContactViewTable;
    } 
	
     public function getContactsTable()
     {
     	if (!$this->contactsTable) {
     		$sm = $this->getServiceLocator();
     		$this->contactsTable = $sm->get('Contacts\Model\ContactsTable');
     	}
     	return $this->contactsTable;
     }
     
     public function getPersonTable()
     {
     	if (!$this->personTable) {
     		$sm = $this->getServiceLocator();
     		$this->personTable = $sm->get('Contacts\Model\PersonTable');
     	}
     	return $this->personTable;
     }
     
     public function getDetailsTable()
     {
     	if (!$this->detailsTable) {
     		$sm = $this->getServiceLocator();
     		$this->detailsTable = $sm->get('Contacts\Model\DetailsTable');
     	}
     	return $this->detailsTable;
     }

     public function addAction()
     {
     	$form = new ContactForm();
     	$form->get('submit')->setValue('Add');
     	
     	$request = $this->getRequest();
     	if ($request->isPost()) {
     		$person = new Person();
     		$form->setInputFilter($person->getInputFilter());
     		$form->setData($request->getPost());
     		
     		if ($form->isValid()) {
     			$person->exchangeArray($form->getData());
     			$testing=$form->getData();
     			$this->getPersonTable()->savePerson($person);
     	
     			// Redirect to list of albums
     			return $this->redirect()->toRoute('contacts');
     		
     		}
     		     		
     	}
     	return array('form' => $form);
     	
     }
     
         
     public function detailsAction()
     {
     	$id = (int) $this->params()->fromRoute('id',0);
     	if(!$id){
     		return $this->redirect()->toRoute('contacts', array(
     			'action' => 'index'
     		));
     	}
     	
     	try{
     		$details = $this->getDetailsTable()->getDetails($id);
     	}
     	catch (\Exception $ex){
     		return $this->redirect()->toRoute('contacts', array(
     			'action'=>'index'
     		));
     	}
     	
     	$form = new DetailsForm();
     	$form->bind($details);
     	
     	return array(
     		'id'=>$id,
     		'form'=>$form,
     	);
     	
     }

     public function editAction()
     {
     	$id = (int) $this->params()->fromRoute('id', 0);
     	if (!$id) {
     		return $this->redirect()->toRoute('contacts', array(
     				'action' => 'add'
     		));
     	}
     	
     	// Get the Album with the specified id.  An exception is thrown
     	// if it cannot be found, in which case go to the index page.
     	try {
     		$person = $this->getPersonTable()->getPerson($id);
     	}
     	catch (\Exception $ex) {
     		return $this->redirect()->toRoute('contacts', array(
     				'action' => 'index'
     		));
     	}
     	
     	$form  = new ContactForm();
     	$form->bind($person);
     	$form->get('submit')->setAttribute('value', 'Edit');
     	
     	$request = $this->getRequest();
     	if ($request->isPost()) {
     		$form->setInputFilter($person->getInputFilter());
     		$form->setData($request->getPost());
     	
     		if ($form->isValid()) {
     			$this->getPersonTable()->savePerson($person);
     	
     			// Redirect to list of albums
     			return $this->redirect()->toRoute('contacts');
     		}
     	}
     	
     	return array(
     			'id' => $id,
     			'form' => $form,
     	);
     }

     public function deleteAction()
		{
         $id = (int) $this->params()->fromRoute('id', 0);
         if (!$id) {
             return $this->redirect()->toRoute('contacts');
         }

         $request = $this->getRequest();
         if ($request->isPost()) {
             $del = $request->getPost('del', 'No');

             if ($del == 'Yes') {
                 $id = (int) $request->getPost('id');
                 $this->getPersonTable()->deletePerson($id);
             }

             // Redirect to list of albums
             return $this->redirect()->toRoute('contacts');
         }

         return array(
             'id'    => $id,
             'person' => $this->getPersonTable()->getPerson($id)
         );
     	}
     
     public function cloneAction()
     {
     	$id = (int) $this->params()->fromRoute('id', 0);
     	if (!$id) {
     		return $this->redirect()->toRoute('contacts', array(
     				'action' => 'add'
     		));
     	}
     	
     	// Get the Album with the specified id.  An exception is thrown
     	// if it cannot be found, in which case go to the index page.
     	try {
     		$person = $this->getPersonTable()->getPerson($id);
     	}
     	catch (\Exception $ex) {
     		return $this->redirect()->toRoute('contacts', array(
     				'action' => 'index'
     		));
     	}
     	
     	$form  = new ContactForm();
     	$form->bind($person);
     	$form->get('submit')->setAttribute('value', 'Clone');
     	
     	$request = $this->getRequest();
     	if ($request->isPost()) {
     		$form->setInputFilter($person->getInputFilter());
     		$form->setData($request->getPost());
     	
     		if ($form->isValid()) {
     			$this->getPersonTable()->savePerson($person);
     	
     			// Redirect to list of albums
     			return $this->redirect()->toRoute('contacts');
     		}
     	}
     	
     	return array(
     			'id' => $id,
     			'form' => $form,
     	);
     }
}
